package com.bw1806.shop.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @描述
 * @创建人 wangyue
 * @创建时间2020/11/1810:51
 */
@RestController
@RequestMapping("file")
public class FileController {

    @Value("${file.filepath}")
    private String filepath;

    @Value("${file.baseurl}")
    private String baseurl;


    @RequestMapping("uplaod")
    public Map<String,String> uplaod(MultipartFile multipartFile){
        Map<String,String> map = new HashMap<>();
        //防止用户上传文件名称重复；用uuid代替原理的名字
        //获取文件真实名称
        String name= multipartFile.getOriginalFilename();
        //获取名称后缀
        String affix = name.substring(name.lastIndexOf("."));
        String savename=UUID.randomUUID().toString()+affix;
        try {
            multipartFile.transferTo(new File(filepath+"\\"+savename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("imgsrc",baseurl+"/"+savename);
        return map;
    }
}
